package com.tjh.springbootapi.config;

import com.koala.apiprototype.config.BaseConfig;
import com.koala.apiprototype.web.interceptor.TokenInterceptor;
import com.tjh.springbootapi.common.ApiConstants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

@Configuration
@Import(BaseConfig.class)
public class ApiConfig implements WebMvcConfigurer {

    @Value("${api.queue.vehicle:#{null}}")
    private String queue_vehicle;

    //请求上下文拦截器
    @Bean
    public RequestContextInterceptor requestContextInterceptor() {
        return new RequestContextInterceptor();
    }

    //TODO 开发环境暂停token验证，发布前要开启
    //token验证拦截器，不需要token验证的方法加@IgnoreValidate
    @Bean
    public TokenInterceptor tokenInterceptor() {
        return new TokenInterceptor(ApiConstants.HEADER_API_TOKEN, ApiConstants.HEADER_USER_ID, ApiConstants.REDIS_KEY_USER_TOKEN_PREFIX);
    }

    /**
     * 配置拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> staticResource = Arrays.asList("/swagger-ui.html", "/webjars/**", "/swagger-resources/**");
        registry.addInterceptor(tokenInterceptor()).excludePathPatterns(staticResource);
        registry.addInterceptor(requestContextInterceptor()).excludePathPatterns(staticResource);
    }
}
